package com.yh.controller.api;

import com.yh.entity.Movie;
import com.yh.entity.MovieCountry;
import com.yh.entity.MovieType;
import com.yh.service.ElasticsearchService;
import com.yh.service.MovieService;
import com.yh.utils.Constants;
import com.yh.vo.CustomPageAndNavigate;
import com.yh.utils.TransformSearchResult;
import com.yh.vo.MovieVO;
import com.yh.vo.ResponseVO;
import com.yh.vo.ScreenVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/api/movie")
public class MovieWeb {
    @DubboReference(check = false)
    private MovieService movieService;
    @DubboReference(check = false)
    private ElasticsearchService elasticsearchService;

    /**
     * 查询所有的电影信息
     * 服务地址 http://localhost:8080/api/movie/all
     */
    @RequestMapping("all")
    public ResponseVO findMovieAll() {
        List<Movie> movieList = movieService.findMovieAll();
        return ObjectUtils.isEmpty(movieList) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(movieList);
    }

    /**
     * 查询简单的影片信息
     * 服务地址 http://localhost:8080/api/movie/movie?movieId=1
     */
    @RequestMapping("movie")
    public ResponseVO findMovieById(Integer movieId) {
        Movie movie = movieService.findMovieById(movieId);
        return ObjectUtils.isEmpty(movie) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT) :
                ResponseVO.buildSuccessInstance(movie);
    }

    /**
     * 查询电影详情（影片信息+演职人员）
     * 服务地址 http://localhost:8080/api/movie/detail?movieId=1
     */
    @RequestMapping("detail")
    public ResponseVO findMovieDetailById(Integer movieId) {
        MovieVO movieVO = movieService.findMovieDetailByIdFromRedis(movieId);
        return ObjectUtils.isEmpty(movieVO) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT) :
                ResponseVO.buildSuccessInstance(movieVO);
    }

    /**
     * 查询影片的信息需要处理主演的姓名（将所有主演的姓名拼接成串，如：'甄子丹 肖央'）
     * 服务地址 http://localhost:8080/api/movie/movie/star?movieId=1
     */
    @RequestMapping("/movie/star")
    public ResponseVO findMovieByIdNeedStarName(Integer movieId) {
        MovieVO movieVO = movieService.findMovieByIdNeedStarName(movieId);
        return ObjectUtils.isEmpty(movieVO) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT) :
                ResponseVO.buildSuccessInstance(movieVO);
    }

    /**
     * 查询所有的电影类型
     * 服务地址 http://localhost:8080/api/movie/type/all
     */
    @RequestMapping("/type/all")
    public ResponseVO findMovieTypeAll() {
        List<MovieType> movieTypeList = movieService.findMovieTypeAll();
        return ObjectUtils.isEmpty(movieTypeList) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(TransformSearchResult.siphonIntoDefault(mt -> mt.getTypeName(), movieTypeList));
    }

    /**
     * 查询所有的制片地区
     * 服务地址 http://localhost:8080/api/movie/country/all
     */
    @RequestMapping("/country/all")
    public ResponseVO findMovieCountryAll() {
        List<MovieCountry> movieCountryList = movieService.findMovieCountryAll();
        return ObjectUtils.isEmpty(movieCountryList) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(TransformSearchResult.siphonIntoDefault(mc -> mc.getCountryName(), movieCountryList));
    }

    /**
     * 从搜索引擎中根据指定的多个字段搜索关键字（具体字段详见db.properties配置文件）
     * 服务地址 http://localhost:8080/api/movie/search
     */
    @RequestMapping("search")
    public ResponseVO searchOfKeyWordFromEs(String keyWord, int pageNum, int pageSize) {
        CustomPageAndNavigate page = elasticsearchService.searchOfKeyWord(keyWord, pageNum, pageSize);
        return ObjectUtils.isEmpty(page) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(page);
    }

    /**
     * 影片的高级筛选（根据影片的类型、制片区域和上映时间筛选;使用搜索引擎查询）
     * 服务地址 http://localhost:8080/api/movie/screening
     */
    @RequestMapping("screening")
    public ResponseVO advancedScreening(@RequestBody ScreenVO screenVO) {
        CustomPageAndNavigate page = elasticsearchService.searchByTypeAndCountryAndYear(screenVO);
        return ObjectUtils.isEmpty(page) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(page);
    }
}
